package com.test.springboot.jpa.service.impl;

import com.test.springboot.jpa.dao.UserDao;
import com.test.springboot.jpa.pojo.User;
import com.test.springboot.jpa.service.UserCrudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/8/18 3:14
 * @package com.test.springboot.jpa.service.impl
 * @description 文件说明
 */

@Service
@Slf4j
public class UserCrudServiceImpl implements UserCrudService {

    @Autowired
    private UserDao userDao;


    @Override
    public long countUser() {
        return userDao.count();
    }

    @Override
    public boolean existUser(Example<User> userExample) {
        return userDao.exists(userExample);
    }

    @Override
    public boolean existByPrimaryKey(Long userId) {
        return userDao.existsById(userId);
    }

    @Override
    public List<User> selectAllUser() {
        return userDao.findAll();
    }

    @Override
    public User selectByPrimaryKey(Long userId) {
        User user = null;
        try {
             user = userDao.findById(userId).get();
        }catch (NoSuchElementException e){
            log.info("java.util.NoSuchElementException: No value present:没有这条记录：====>>> user_id = {}",userId);
        }
        catch (Exception e) {
            log.info("Exception: 有其他错误");
        }
        return user;
    }

    @Override
    public List<User> selectByUserExample(Example<User> userExample) {
        List<User> userExampleList = userDao.findAll(userExample);
        return userExampleList;
    }

    @Override
    public boolean insertUser(User user) {
        User createUser = null;
        try {
            createUser = userDao.save(user);
            log.info("insert user info log ===>>> {}",createUser);
        }catch (DataIntegrityViolationException e){
            log.info("org.springframework.dao.DataIntegrityViolationException: 唯一值冲突");
        }
        catch (Exception e) {
            log.info("Exception: 其他错误");
            e.printStackTrace();
        }
        return createUser != null;
    }

    @Override
    public boolean insertAllUsers(List<User> userList) {
        List<User> createUserList = userDao.saveAll(userList);
        log.info("insert into nums log ===>>> {}",createUserList.size());
        return createUserList.size() != 0;
    }

    @Override
    public boolean updateUser(User user) {
        if (user.getUserId() == null){
            log.info("没有主键，请执行 insert 操作");
            return false;
        }
        User startUser = selectByPrimaryKey(user.getUserId());
        userDao.save(user);
        User updateUser = selectByPrimaryKey(user.getUserId());
        return startUser != updateUser;
    }

    @Override
    public boolean deleteAll() {
        long startNum = countUser();

        userDao.deleteAll();
        userDao.flush();

        long endNum = countUser();

        return startNum != endNum;
    }

    @Override
    public boolean deleteByPrimaryKey(Long userId) {
        long startCount = userDao.count();

        userDao.deleteById(userId);

        long endCount = userDao.count();

        return startCount != endCount;
    }

    @Override
    public boolean deleteInBranch(List<User> userList) {
        long startCount = userDao.count();
        userDao.deleteInBatch(userList);

        long endCount = userDao.count();

        return startCount != endCount;
    }

    /**
     * TODO need to test
     * @param user
     * @return
     */
    @Override
    public boolean deleteByUser(User user) {
        return false;
    }
}
